package com.baike.controller;


import com.baike.data.Result;
import com.baike.em.ConstantEnum;
import com.baike.entity.TbStudent;
import com.baike.mapper.TbStudentMapper;
import com.baike.service.ITbStudentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.netty.util.Constant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.naming.Name;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 李斌
 * @since 2021-10-29
 */
@RequestMapping("studentController")
@RestController
@RefreshScope//开启自动刷新
@Api(value = "学生管理服务")
public class TbStudentController {

    @Autowired
    private TbStudentMapper tbStudentMapper;
    @Autowired
    private ITbStudentService iTbStudentService;

    @ApiOperation(value = "学生增加功能")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tbStudent",value = "学生实体类",required = true,paramType = "body")
    })
    @PostMapping("insert")
    public Result add(@RequestBody TbStudent tbStudent){
        int insert = tbStudentMapper.insert(tbStudent);
        return Result.build(ConstantEnum.SUCCESS);
    }

    @ApiOperation(value = "查询所有数据")
    @GetMapping("findAll")
    public Result findAll(){
        List<TbStudent> tbStudents = tbStudentMapper.selectList(null);
        return Result.build(ConstantEnum.SUCCESS).setResult(tbStudents);
    }

    @ApiOperation(value = "查询粉丝功能,(讲师)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "teacherId",value = "老师Id",required = true,paramType = "query"),
    })
    @GetMapping("findById")
    public Result findById(@RequestParam("teacherId")Long teacherId){
//        TbStudent tbStudent = tbStudentMapper.selectById(studentId);
        List<TbStudent> studentList = tbStudentMapper.selectByStudent(teacherId);
        int size = studentList.size();
        return Result.build(ConstantEnum.SUCCESS).setResult(size);
    }


    @ApiOperation(value = "修改资料及状态(运营商)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tbStudent",value = "学生类",required = true,paramType = "body"),
            @ApiImplicitParam(name = "studentId",value = "学生Id",required = true,paramType = "query")
    })
    @GetMapping("update")
    public Result update(@RequestBody TbStudent tbStudent,@RequestParam("studentId")Long studentId){
        int i = tbStudentMapper.updateById(tbStudent);
        if (i > 0){
            return Result.build(ConstantEnum.SUCCESS);
        }
        return Result.build(ConstantEnum.ERROR);
    }


    @ApiOperation(value = "批量删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "studentId",value = "学生id",required = true,paramType = "query"),
    })
    @DeleteMapping("delete")
    public Result deleteBatch(@RequestParam("studentId")Long[] studentId){
        boolean b = iTbStudentService.removeByIds(Arrays.asList(studentId));
        if (!b){
            System.out.println("删除失败");
        }
        return Result.build(ConstantEnum.SUCCESS);
    }
}
